﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GandA.Component.DataAccess.SQLServer;
using GandAInvitaciones.BusinessEntity;
using System.Data;

namespace GandAInvitaciones.DAO
{
    public class CommonServiceDAO
    {
        public static void InsertaContacto(Contacto obj)
        {
            SQLParameterCollection paramsSql = new SQLParameterCollection();

            paramsSql.Add(new SQLParameter("@psNombre", obj.Nombre, SQLParameter.vEnumSqlDbType.NVarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psTelefono", obj.Telefono, SQLParameter.vEnumSqlDbType.NVarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psCorreo", obj.Correo, SQLParameter.vEnumSqlDbType.NVarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psAsunto", obj.Asunto, SQLParameter.vEnumSqlDbType.NVarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psIpMod", obj.IpMod, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));

            try
            {
                new SQL().ExecuteNonQueryStoreProcedure("Inv.spInsertaContacto", paramsSql);
            }
            catch (SQLException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static void InsertaVisita(Visita obj)
        {
            SQLParameterCollection paramsSql = new SQLParameterCollection();

            paramsSql.Add(new SQLParameter("@psIpVisita", obj.IpVista, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@ptFechaVisita", obj.FechaVisita, SQLParameter.vEnumSqlDbType.DateTime, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psCiudad", obj.Ciudad, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psEstado", obj.Estado, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psPais", obj.Pais, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psExplorador", obj.Explorador, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@pnEsMobile", obj.EsMobile, SQLParameter.vEnumSqlDbType.Bit, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psSistemaOperativo", obj.SistemaOperativo, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psReferenciaUrl", obj.ReferenciaUrl, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psLatitud", obj.Latitud, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psLongitud", obj.Longitud, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));

            try
            {
                new SQL().ExecuteNonQueryStoreProcedure("Inv.spInsertaVisita", paramsSql);
            }
            catch (SQLException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static void InsertaBoletin(Boletin obj)
        {
            SQLParameterCollection paramsSql = new SQLParameterCollection();

            paramsSql.Add(new SQLParameter("@psCorreo", obj.CorreoElectronico, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psIpMod", obj.IpMod, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));

            try
            {
                new SQL().ExecuteNonQueryStoreProcedure("Inv.spInsertaCorreoBoletin", paramsSql);
            }
            catch (SQLException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static Cupon InsertaCuponGenerado(Cupon obj)
        {
            SQLParameterCollection paramsSql = new SQLParameterCollection();

            paramsSql.Add(new SQLParameter("@psCorreo", obj.CorreoElectronico, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psIpMod", obj.IpMod, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));

            try
            {
                DataSet ds = new SQL().ExecuteQueryStoreProcedure("Inv.spInsertaCuponGenerado", paramsSql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    obj.CuponGenerado = ds.Tables[0].Rows[0]["Cupon"].ToString();
                    obj.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                    obj.FechaVigencia = ds.Tables[0].Rows[0]["FechaVigencia"].ToString();
                }

                return obj;
            }
            catch (SQLException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static string Utilizacupon(Cupon obj)
        {
            SQLParameterCollection paramsSql = new SQLParameterCollection();

            paramsSql.Add(new SQLParameter("@psCodigo", obj.CuponGenerado, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@pnFechaVigencia", obj.FechaVigencia, SQLParameter.vEnumSqlDbType.Int, SQLParameter.vEnumParameterDirection.vInput));
            paramsSql.Add(new SQLParameter("@psIpMod", obj.IpMod, SQLParameter.vEnumSqlDbType.VarChar, SQLParameter.vEnumParameterDirection.vInput));

            try
            {
                DataSet ds = new SQL().ExecuteQueryStoreProcedure("Inv.spAplicaCupon", paramsSql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return ds.Tables[0].Rows[0]["Mensaje"].ToString();
                }

                return string.Empty;
            }
            catch (SQLException ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
